package com.vnaloha.common.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.vnaloha.common.model.Product;
import com.vnaloha.util.CustomHibernateDaoSupport;

@Repository
public class ProductDAOImpl extends CustomHibernateDaoSupport implements ProductDAO{
	
	private Logger logger = LoggerFactory.getLogger(ProductDAOImpl.class);

	@Override
	public Product get(int id) {
		Product product = (Product) getHibernateTemplate().get(Product.class, id);

		if (null != product) {
			return product;
		}
		logger.error("Error when get product by id " + id);
		return null;
	}

	@Override
	public boolean save(Product product) {
		 getHibernateTemplate().save(product);
		 return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Product> list() {
		List<Product> list = null;
		try {
			list = getHibernateTemplate().find(" from Product order by id desc");
		} catch (HibernateException e) {
			logger.error("Error when get list product " + e, e);
		}
		return list;
	}

	@Override
	public List<Product> list(int start, int max) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean delete(int id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update(Product product) {
		 getHibernateTemplate().update(product);
		 return true;
	}

}
